#coding=utf-8
import wx
import tree, fetcher
from pyExcelerator import *
from fetcher import *

def get_suggesttree(query, fetch, parent=None, out=None):
    
    root = tree.Node(word=query, parent=parent)
    
    data = fetch(query)
    
    if not data:return root
    
    for d in data:
    
        if out:
            out.AppendText('%s\n'%d)
        print d
        t = get_suggesttree(d, fetch, root, out)
        root.add_child(t)
        
    return root

def sgtree2sheet_as_list(sgtree, sheet):
    sheet.write(sgtree.z, 0, sgtree.word)    
    
    if not sgtree.children:return
    
    for child in sgtree.children:
        sgtree2sheet_as_list(child, sheet)
    
def sgtree2sheet_as_tree(sgtree, sheet):
    
    x = sgtree.location.x
    y = sgtree.location.y
    
    sheet.write(y, x+2, sgtree.word)
    
    if not sgtree.children:return
    
    for child in sgtree.children:
        sgtree2sheet_as_tree(child, sheet)

def sgtree2xls(sgtree, workbook, sheet_name):

    sgtree.mark_number()
    sgtree.mark_number2()
   
    sheet = workbook.add_sheet(sheet_name)
    
    sgtree2sheet_as_list(sgtree, sheet)
    sgtree2sheet_as_tree(sgtree, sheet)
    

fetchers = [google_cn, yahoo_cn, ask]#, baidu]
class MainFrame(wx.Frame):
    
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'SuggestFetcher', 
                size=(400, 300))
        panel = wx.Panel(self, -1)
        
        sizer = wx.GridBagSizer(hgap=5, vgap=5)
        
        #self.ch = wx.Choice(panel, -1, choices=choices)
        #sizer.Add(self.ch, pos=(0, 0))
        #self.ch.SetSelection(0)
        
        self.st = wx.StaticText(panel, -1, label=unicode('请输入关键词:', 'u8'), 
                                style=wx.ALIGN_CENTER)
        sizer.Add(self.st, pos=(0, 0))
        
        self.tc = wx.TextCtrl(panel, -1, "harvard", 
                size=(175, -1))
        self.tc.SetInsertionPoint(0)
        sizer.Add(self.tc, pos=(0, 1))
        
        self.bt = wx.Button(panel, -1, "go")
        self.Bind(wx.EVT_BUTTON, self.OnClick, self.bt)
        sizer.Add(self.bt, pos=(0, 2))
        
        self.mt = wx.TextCtrl(panel, -1,
                    size = (350, 200),
                    style=wx.TE_MULTILINE|wx.TE_READONLY)
        sizer.Add(self.mt, pos=(1, 0),span=(3,3) )
        
        panel.SetSizer(sizer)
       
        
    def OnClick(self, event):
        dlg = wx.FileDialog(self, style=wx.SAVE,
                message="chose output Excel file")
                
        if dlg.ShowModal() == wx.ID_OK:
            fn = dlg.GetPath()
            if fn.split('.')[-1] != 'xls':
                fn = fn + '.xls'
            
            self.mt.Clear()
            
            wb = Workbook()
            query = self.tc.GetValue()
            
            print query
            
            for fetcher in fetchers:
                
                tree.x = 0
                tree.y = 0
                tree.z = 0
                
                print fetcher.__name__
                    
                self.mt.AppendText('%s:\n'%fetcher.__name__)
                sgt = get_suggesttree(query, fetcher, out=self.mt)
                self.mt.AppendText('\n\n')
                
                sgtree2xls(sgt, wb, fetcher.__name__)
                
            wb.save(fn)    
            message_dlg = wx.MessageDialog(None, "success!",
                          'A Message Box',
                          wx.OK)
            message_dlg.ShowModal()
            
if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = MainFrame()
    frame.Show()
    app.MainLoop()


    
    
    
